微信开发回复用户消息的代码实例分享
时间 :
2017-04-18,10:10:49
编辑 ::ADMIN
前段时间开发了公司的微信
这里做个知识总结分享下经验,望一起学习。。。。。
感觉后面写个系列的最好了 。。。。
企业需求:
给指定企业用户发送消息:如考勤信息、 企业通知、个人信息推送等等,
/// /// <param>要发送的人ID /// <param>消息 private void SendMessage(string UserID, string StrMessg) { Como.LogUtil.WriteLog("回复用户" + UserID + "消息"); string Access_Token = Como.GetAccessToken.GetAccess_token(); if (Access_Token == "") Como.LogUtil.WriteException("SendMessage 未能成功加载Access_Token"); string Text = @"{ ""touser"":"; Text += '"' + UserID + '"'; Text += "," + '"' + @"msgtype"": ""text"", ""agentid"": ""5"", ""text"": { ""content"":"; Text += '"' + StrMessg + '"'; Text += @"}, ""safe"": ""0"" }"; ; string url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", Access_Token); string strResult = Como.Tools.GetPage(url, Text); JArray ja = (JArray)JsonConvert.DeserializeObject("[" + strResult + "]"); string Error = ""; try { if (strResult.Contains("errcode")) { Error = ja[0]["errcode"].ToString(); } } catch (Exception ex) { Como.LogUtil.WriteException("获取strResult,未获取到错误信息" + ex.Message.ToString()); } string errcode = ja[0]["errcode"].ToString(); string errmsg = ja[0]["errmsg"].ToString(); if (errcode == "0" && errmsg == "ok") { Como.LogUtil.WriteLog("回复成功!"); } else { Como.LogUtil.WriteLog("回复失败!"); Como.LogUtil.WriteException("回复失败:SendMessage:" + strResult); } }